Sähköpostin salaaminen PGP:llä

GDPR:n myötä plaintext sähköposteissa ei saa lähettää enää henkilötietoja. Esimerkiksi webbisivulla html lomakkeella kerätyt tiedot lähetetään toisinaan sähköpostilla eteenpäin. Nyt postit pitää kryptata. Edullinen tapa sähköpostin salaamiseen on pgp-salaus.

pgp-avainparin luominen

Ensiksi luodaan vastaanottajalle avainpari. Asennetaan Enigmail laajennus.

Sitten luodaan lähettäjälle avainpari

blah blah...

php-scripti joka lähettää pgp-salatun postin

Tässä scripti joka lähettää pgp-salatun viestin. Tämä scripti käyttää lisäksi lähteävässä sähköpostissa smtp-authia.
<php
// Install depencies
//  sudo apt-get install php-gnupg php-mail

// Include mail extension
require_once "Mail.php";

// Set PGP keyring
putenv('GNUPGHOME=/var/www/html/forms/.gnupg');

// create new GnuPG object
$gpg = new gnupg();

// throw exception if error occurs
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);

// recipient's email address
$recipient = 'forms.receiver@example.org';

// plaintext message
$plaintext = "Hello,\nThis is gnupg encrypted email.\n";

try {
  // find key matching email address
  $gpg->addencryptkey($recipient);
  // encrypt plaintext message
  $ciphertext = $gpg->encrypt($plaintext);

  // SMTP mail sender and subject
  $from = "www-form data <www-form@www.example.org>";
  $subject = "www-form pgp-encrypted data";

  // SMTP-server auth
  $smtp_host = "smtp.example.org";
  $smtp_username = "myusername";
  $smtp_password = "mypassword";

  // Send email
  $headers = array ('From' => $from, 'To' => $recipient, 'Subject' => $subject,  'Content-Type' => 'text/plain; charset=UTF-8');
  $smtp = Mail::factory('smtp', array ('host' => $smtp_host, 'auth' => true, 'username' => $smtp_username, 'password' => $smtp_password));
  $mail = $smtp->send($recipient, $headers, $ciphertext);
  if (PEAR::isError($mail)) {
	echo($mail->getMessage());
  } else {
	echo("Message successfully sent!\n");
  }
} catch (Exception $e) {
  die('ERROR: ' . $e->getMessage());
}
?>